package cn.bb.test;

import cn.bb.biz.PersonBiz;
import cn.bb.biz.PersonBizImpl;
import cn.bb.pojo.Person;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    ApplicationContext app = null;
    PersonBiz pb = null;
    @Before
    public void test1(){
        app=new ClassPathXmlApplicationContext("spring.xml");
                                        //必须是接口
        pb = app.getBean("personBiz", PersonBiz.class);
    }
    @Test
    public void test2(){//测试添加

        Person person = new Person(); person.setUsername("lili2");person.setUserpassword("123454");
        person.setAge(20);
        pb.addPerso(person);
    }
    @Test
    public void test3(){//get方法获取对应类的信息
        //PersonBiz pb = app.getBean("personBiz", PersonBizImpl.class);
        System.out.println(pb.gets(3));
    }
    @Test
    public void test4(){//修改
        //PersonBiz pb = app.getBean("personBiz", PersonBizImpl.class);
        Person person = new Person(2,"李华","123",23);
        pb.updatePerson(person);
    }
    @Test
    public void test5(){//删除
        //PersonBiz pb = app.getBean("personBiz", PersonBizImpl.class);
        pb.delPerson(2);
    }
    @Test
    public void test6(){//批量添加 并触发事务
        //PersonBiz pb = app.getBean("personBiz", PersonBizImpl.class);
        List<Person> list = new ArrayList();
        for(int i=0;i<4;i++){
            Person p1 = new Person();
            if(i == 2){
                p1.setUsername(null);
                p1.setUserpassword("111");
                p1.setAge(20);
            }else{
                p1.setUsername("ceshi"+i);
                p1.setUserpassword("111");
                p1.setAge(20);
            }
            list.add(p1);
        }
        pb.addPersons(list);
    }
}
